package com.ptswitch.material.pages.material;

import java.util.List;

import org.apache.tapestry5.EventConstants;
import org.apache.tapestry5.annotations.ActivationRequestParameter;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SetupRender;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.hxzon.project.SplitPageInfo;
import org.slf4j.Logger;

import com.ptswitch.material.model.material.Material;
import com.ptswitch.material.pages.base.PageBase;
import com.ptswitch.material.service.MaterialService;
import com.ptswitch.material.util.security.PermissionUtil;

public class SearchMaterial extends PageBase {
    @Inject
    private Logger logger;
    @Inject
    private MaterialService materialSer;
    @Property
    private SplitPageInfo page = new SplitPageInfo();
    //
    @Property
    private List<Material> list;
    @ActivationRequestParameter
    @Property
    private String like = "";

    public Object withInit(String like, long size, long first) {
        this.like = like;
        page.setCurrentPage(first);
        page.setPageSize(size);
        return this;
    }

    @OnEvent(value = EventConstants.ACTIVATE)
    Object init(long size, long first) {
        checkPermitted(PermissionUtil.MaterialList);
        withInit(like, size, first);
        return null;
    }

    @SetupRender
    void display() {
        page.setRowCount(materialSer.countLike(like));
        list = materialSer.findLike(like, page.getCurrentRow(), page.getPageSize());
//		page.setRowCount(materialSer.countSearch(like));
//		list = materialSer.search(like, page.getCurrentRow(), page.getPageSize());
    }

    @OnEvent(value = EventConstants.PASSIVATE)
    Object onPassivate() {
        return new Object[] { page.getPageSize(), page.getCurrentPage() };
    }

}
